package com.example.androidtest4

import android.app.ComponentCaller
import android.content.Intent
import android.graphics.BitmapFactory
import android.icu.number.NumberRangeFormatter
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.activity.FullyDrawnReporter
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import java.io.File
import kotlin.let

const val FIRST_MSG="FIRST_MSG"
const val SECOND_MSG="SECOND_MSG"
const val IMAGE_PATH="IMAGE_PATH"

class MainActivity : AppCompatActivity() {
    private var maxScore: Int = 0
    private lateinit var maxTextView: TextView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        val button5 = findViewById<Button>(R.id.button5)
        val button6 = findViewById<Button>(R.id.button6)
        maxTextView=findViewById(R.id.score)
        //记录第一个页面游戏最高分的内容，初始值是0，再到后面进行游戏之后的得分作比较
        maxTextView.text="游戏最高分：$maxScore"
        //跳转到游戏页面
        button6.setOnClickListener {
            val intent = Intent(this, MainActivity2::class.java)
            startActivityForResult(intent,2)
        }

        //跳转到修改信息页面
        button5.setOnClickListener {
            val intent = Intent(this, MainActivity3::class.java)
            startActivityForResult(intent,0)
        }


        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        val name=findViewById<EditText>(R.id.name)
        val sno=findViewById<EditText>(R.id.sno)
        val phone=findViewById<EditText>(R.id.phone)
        val email=findViewById<EditText>(R.id.email)
        val sex=findViewById<EditText>(R.id.sex)
        val hobby=findViewById<EditText>(R.id.hobby)
        //以数组的形式传回多个值，所以按照数组形式给每一个信息获取值
        if(requestCode==0&&resultCode==RESULT_OK){
            val info=data?.getStringArrayExtra("FIRST_MSG")
            info?.let{
                name.setText(it[0])
                sno.setText(it[1])
                email.setText(it[2])
                phone.setText(it[3])
                sex.setText(it[4])
                hobby.setText("爱好："+it[5])
            }
            val imagePath=data?.getStringExtra("IMAGE_PATH")
            val imageView=findViewById<ImageView>(R.id.imageView)
            //图片地址获取
            if(!imagePath.isNullOrEmpty()) {
                val imagefile= File(imagePath)
                if(imagefile.exists()){
                    val bitMap= BitmapFactory.decodeFile(imagePath)
                    imageView.setImageBitmap(bitMap)
                }
            }
        }
        //获取本次游戏最高分和之前记录的游戏最高分作比较
        if(requestCode==2&&resultCode==RESULT_OK&&data!=null){
            val current =data.getIntExtra("SECOND_MSG",0)
            if(current>maxScore){
                maxScore=current
                maxTextView.text="游戏最高分:$maxScore"
            }

        }
    }
}
